#!/bin/bash

enableMail=$(awk '$1=="enableMail" {print $3}' servermanager.conf)
mailAdd=$(awk '$1=="mailAdd" {print $3}' servermanager.conf)
enableRestart=$(awk '$1=="enableRestart" {print $3}' servermanager.conf)
enableObRestart=$(awk '$1=="enableObRestart" {print $3}' servermanager.conf)

mode=$1
shift

case $mode in
	1)
		serverStatus=1
		while [ $serverStatus -eq 1 ]; do
			./checkStatus observermode authserver
			serverStatus=$?
			sleep 30 
		done
		
		echo AuthServer crashed! $(date) >> crash.log 
		if [ $enableMail -eq 1 ]; then
			echo AuthServer crashed! $(date) | mail -s"AuthServer crashed" $mailAdd
		fi	
		if [ $enableRestart -eq 1 ]; then
			sleep 5
			./start authserver
		fi
		if [ $enableObRestart -eq 1 ]; then
			sleep 5 
			screen -AmdS Observer ./observer 1 authserver 
		fi
		
		
	;;
	2)
		serverStatus=2
		while [ $serverStatus -eq 2 ]; do
			./checkStatus observermode worldserver
			serverStatus=$?
			sleep 30 
		done

		echo WorldServer crashed! $(date) >> crash.log 
		if [ $enableMail -eq 1 ]; then
			echo WorldServer crashed! $(date) | mail -s"WorldServer crashed" $mailAdd
		fi
		if [ $enableRestart -eq 1 ]; then
			sleep 5
			./start worldserver 
		fi
		if [ $enableObRestart -eq 1 ]; then
			sleep 5 
			screen -AmdS Observer ./observer 2 worldserver
		fi

	
	;;
	3)
		serverStatus=3
		while [ $serverStatus -eq 3 ]; do
			./checkStatus observermode authserver worldserver
			serverStatus=$?
			sleep 30 
		done

		if [ $serverStatus -eq 1 ]; then
			echo WorldServer crashed! $(date) >> crash.log 
			if [ $enableMail -eq 1 ]; then
				echo WorldServer crashed! $(date) | mail -s"Worldserver crashed" $mailAdd
			fi
			if [ $enableRestart -eq 1 ]; then
				sleep 5
				./start worldserver 
			fi 
			if [ $enableObRestart -eq 1 ]; then
				sleep 5 
				screen -AmdS Observer ./observer 3 authserver worldserver
			fi
		else
			echo AuthServer crashed! $(date) >> crash.log
			if [ $enableMail -eq 1 ]; then
				echo AuthServer crashed! $(date) | mail -s"AuthServer crashed" $mailAdd
			fi
			if [ $enableRestart -eq 1 ]; then
				sleep 5
				./start authserver 
			fi 
			if [ $enableObRestart -eq 1 ]; then
				sleep 5 
				screen -AmdS Observer ./observer 3 authserver worldserver
			fi
		fi

	;;
	4)
		serverStatus=4
		while [ $serverStatus -eq 4 ]; do
			./checkStatus observermode testserver
			serverStatus=$?
			sleep 30 
		done

		echo TestServer crashed! $(date) >> crash.log
		if [ $enableMail -eq 1 ]; then
			echo TestServer crashed! $(date) | mail -s"TestServer crashed" $mailAdd
		fi
		if [ $enableRestart -eq 1 ]; then
			sleep 5
			./start testserver 
		fi
		if [ $enableObRestart -eq 1 ]; then
			sleep 5 
			screen -AmdS Observer ./observer 4 testserver 
		fi
	;;
	5)
		serverStatus=5
		while [ $serverStatus -eq 5 ]; do
			./checkStatus observermode authserver testserver
			serverStatus=$?
			sleep 30 
		done

		if [ $serverStatus -eq 1 ]; then
			echo TestServer crashed! $(date) >> crash.log
			if [ $enableMail -eq 1 ]; then
				echo TestServer crashed! $(date) | mail -s"TestServer crashed" $mailAdd
			fi
			if [ $enableRestart -eq 1 ]; then
				sleep 5
				./start testserver 
			fi
			if [ $enableObRestart -eq 1 ]; then
				sleep 5 
				screen -AmdS Observer ./observer 5 authserver testserver 
			fi
		else
			echo AuthServer crashed! $(date) >> crash.log
			if [ $enableMail -eq 1 ]; then
				echo AuthServer crashed! $(date) | mail -s"AuthServer crashed" $mailAdd
			fi
			if [ $enableRestart -eq 1 ]; then
				sleep 5
				./start authserver 
			fi
			if [ $enableObRestart -eq 1 ]; then
				sleep 5 
				screen -AmdS Observer ./observer 5 authserver testserver 
			fi
		fi
	;;
	6)
		serverStatus=6
		while [ $serverStatus -eq 6 ]; do
			./checkStatus observermode worldserver testserver
			serverStatus=$?
			sleep 30
		done

		if [ $serverStatus -eq 2 ]; then
			echo TestServer crashed! $(date) >> crash.log
			if [ $enableMail -eq 1 ]; then
				echo TestServer crashed! $(date) | mail -s"TestServer crashed" $mailAdd
			fi
			if [ $enableRestart -eq 1 ]; then
				sleep 5
				./start testserver 
			fi
			if [ $enableObRestart -eq 1 ]; then
				sleep 5 
				screen -AmdS Observer ./observer 6 worldserver testserver 
			fi
		else
			echo WorldServer crashed! $(date) >> crash.log
			if [ $enableMail -eq 1 ]; then
				echo WorldServer crashed! $(date) | mail -s"WorldServer crashed" $mailAdd	
			fi
			if [ $enableRestart -eq 1 ]; then
				sleep 5
				./start worldserver 
			fi	
			if [ $enableObRestart -eq 1 ]; then
				sleep 5 
				screen -AmdS Observer ./observer 6 worldserver testserver 
			fi
		fi

	;;
	7)
		serverStatus=7
		while [ $serverStatus -eq 7 ]; do
			./checkStatus observermode authserver worldserver testserver
			serverStatus=$?
			sleep 30
		done

		case $serverStatus in
			1)
				#Nearly impossible that this might happen. But to be sure
				echo World and TestServer crashed! $(date) >> crash.log
				if [ $enableMail -eq 1 ]; then
					echo World and TestServer crashed! $(date) | mail -s"World and TestServer" $mailAdd
				fi
				if [ $enableRestart -eq 1 ]; then
					sleep 5
					./start worldserver 
					./start testserver
				fi
				if [ $enableObRestart -eq 1 ]; then
					sleep 5 
					screen -AmdS Observer ./observer 7 authserver worldserver testserver 
				fi
			;;
			2)
				#Same as above. Quite impossible that this will ever happen!
				echo Auth and TestServer crashed! $(date) >> crash.log
				if [ $enableMail -eq 1 ]; then
					echo Auth and TestServer crashed! $(date) | mail -s"Auth and TestServer" $mailAdd
				fi
				if [ $enableRestart -eq 1 ]; then
					sleep 5
					./start authserver
					./start testserver
				fi
				if [ $enableObRestart -eq 1 ]; then
					sleep 5 
					screen -AmdS Observer ./observer 7 authserver worldserver testserver 
				fi
			;;
			3)
				echo TestServer crashed! $(date) >> crash.log
				if [ $enableMail -eq 1 ]; then
					echo TestServer crashed! $(date) | mail -s"TestServer crashed" $mailAdd
				fi
				if [ $enableRestart -eq 1 ]; then
					sleep 5
					./start testserver
				fi
				if [ $enableObRestart -eq 1 ]; then
					sleep 5 
					screen -AmdS Observer ./observer 7 authserver worldserver testserver 
				fi
			;;
			4)
				echo Auth and WorldServer crashed! $(date) >> crash.log
				if [ $enableMail -eq 1 ]; then
					echo Auth and WorldServer crashed! $(date) | mail -s"Auth and WorldServer" $mailAdd
				fi
				if [ $enableRestart -eq 1 ]; then
					sleep 5
					./start authserver
					./start worldserver
				fi
				if [ $enableObRestart -eq 1 ]; then
					sleep 5 
					screen -AmdS Observer ./observer 7 authserver worldserver testserver 
				fi
			;;
			5)
				echo WorldServer crashed! $(date) >> crash.log
				if [ $enableMail -eq 1 ]; then
					echo WorldServer crashed! $(date) | mail -s"WorldServer crashed" $mailAdd
				fi
				if [ $enableRestart -eq 1 ]; then
					sleep 5
					./start worldserver 
				fi
				if [ $enableObRestart -eq 1 ]; then
					sleep 5 
					screen -AmdS Observer ./observer 7 authserver worldserver testserver 
				fi
			;;
			6)
				echo AuthServer crashed! $(date) >> crash.log
				if [ $enableMail -eq 1 ]; then
					echo AuthServer crashed! $(date) | mail -s"AuthServer crashed" $mailAdd	
				fi
				if [ $enableRestart -eq 1 ]; then
					sleep 5
					./start authserver
				fi
				if [ $enableObRestart -eq 1 ]; then
					sleep 5 
					screen -AmdS Observer ./observer 7 authserver worldserver testserver 
				fi
			;;
		esac
	;;
esac

